
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Sindy
	CreateTime:2008-01-31 13:31:10
*/
public class SysConfigSrv extends IHandler
{

	public SysConfigSrv() 
	{
		super();
	}
	private void deleteSysConfig(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		SysConfig obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = SysConfig.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveSysConfig(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		SysConfig obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = SysConfig.fetch();
    	}
    	else                     //update
    	{
    		obj = SysConfig.fetch(con,id);
    	}
		SysConfigPageData data = (SysConfigPageData)PageData.fromForm(context,SysConfigPageData.class);
		context.setValue("SysConfigPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listSysConfig(DBConnection con,SrvContext context,String path) throws Exception
	{
		SysConfigSearchEvent event = (SysConfigSearchEvent)SearchEvent.fromForm(context,SysConfigSearchEvent.class);
		context.setSessionValue("SysConfigSearchEvent",event);
		context.setNextUrl(path+"SysConfigList.jsp");
	}
	private void backSysConfig(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"SysConfigList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listSysConfig"))
		{
			listSysConfig(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewSysConfig"))
		{
			String id = context.getString("Id",null);
			SysConfig obj = SysConfig.fetch(con,id);
			context.setValue("SysConfig",obj);
			context.setNextUrl(path+"SysConfigView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteSysConfig"))
		{
			context.setErrorUrl(path+"SysConfigList.jsp");
			deleteSysConfig(con,context);
			backSysConfig(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddSysConfig"))
		{
			SysConfig obj = SysConfig.fetch();
			SysConfigPageData data = (SysConfigPageData)PageData.fromTable(obj,SysConfigPageData.class);
			context.setValue("SysConfigPageData",data);
			context.setNextUrl(path+"SysConfigEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateSysConfig"))
		{
			String id = context.getString("Id",null);
			SysConfig obj = SysConfig.fetch(con,id);
			SysConfigPageData data = (SysConfigPageData)PageData.fromTable(obj,SysConfigPageData.class);
			context.setValue("SysConfigPageData",data);
			context.setNextUrl(path+"SysConfigEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopySysConfig"))
		{
			String id = context.getString("Id",null);
			SysConfig obj = SysConfig.copy(SysConfig.fetch(con,id));
			SysConfigPageData data = (SysConfigPageData)PageData.fromTable(obj,SysConfigPageData.class);
			context.setValue("SysConfigPageData",data);
			context.setNextUrl(path+"SysConfigEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveSysConfig"))
		{
			context.setErrorUrl(path+"SysConfigEdit.jsp");
			saveSysConfig(con,context,loginContext);
			backSysConfig(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewSysConfig"))
		{
			context.setErrorUrl(path+"SysConfigEdit.jsp");
			saveSysConfig(con,context,loginContext);
			SysConfig obj = SysConfig.fetch();
			SysConfigPageData data = (SysConfigPageData)PageData.fromTable(obj,SysConfigPageData.class);
			context.setValue("SysConfigPageData",data);
			context.setNextUrl(path+"SysConfigEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListSysConfig"))
		{
			backSysConfig(con,context,path);
			return;
		}
		
		//add by Sindy start

		//add by Sindy end

		Logger.throwException("SysConfigSrv","unknown action_code="+action_code);
	}
}
